from flask import Flask,session
from flask_sqlalchemy import SQLAlchemy
from redis import StrictRedis
from flask_session import Session
from datetime import timedelta

app = Flask(__name__)

# 设置配置信息
class Config(object):
    # 1.debug模式
    DEBUG = True
    SECRET_KEY = "FASDAFASDASD"

    # 2.数据库配置
    SQLALCHEMY_DATABASE_URI = "mysql+pymysql://root:mysql@localhost:3306/infomation17"
    SQLALCHEMY_TRACK_MODIFICATIONS = False

    # 3.redis配置
    REDIS_HOST = "127.0.0.1"
    REDIS_POST = 6379

    # 4.设置session配置信息
    SESSION_TYPE = "redis"
    SESSION_USE_SIGNER = True
    SESSION_REDIS = StrictRedis(host=REDIS_HOST,port=REDIS_POST)
    PERMANENT_SESSION_LIFETIME = timedelta(days=31)

app.config.from_object(Config)

# 创建SQLAlchemy对象，关联app
db = SQLAlchemy(app)

# 创建redis对象
redis_store = StrictRedis(host=Config.REDIS_HOST,port=Config.REDIS_POST)

# 读取app中session配置信息
Session(app)

@app.route("/")
def hello_world():

    redis_store.set("name","laowang")
    print(redis_store.get("name"))

    session["product"] = "lenovo"
    print(session.get("product"))

    return "hello world!"

if __name__ == "__main__":
    app.run()